<?php

require_once 'Zend/Controller/Action.php';

class ProductlistController extends Core_Controller_Action
{
	protected $_style = array ( '/styles/other.css', '/styles/catalog.css' );
	
    protected $_menuEntry = 'catalog';

    protected $_filters = array("new" => Api_Select_Product::FILTER_NEW, "disc" => Api_Select_Product::FILTER_DISCOUNT, "aval" => Api_Select_Product::FILTER_AVAL);

    protected $_sorts = array("code" => Api_Select_Product::SORT_CODE, "name" => Api_Select_Product::SORT_NAME, "price" => Api_Select_Product::SORT_PRICE, "author" => Api_Select_Product::SORT_AUTHOR);

    protected function _initParams()
    {
        $filter = $this->view->filter = $this->_getParam('filter', Api_Select_Product::FILTER_ALL);
        $sort = $this->view->sort = $this->_getParam('sort', Api_Select_Product::SORT_PRICE);
        $is_asc = $this->view->is_asc = $this->_getParam('is_asc', true);
        return array($filter, $sort, $is_asc);
    }

    public function indexAction()
    {
        $this->view->headLink()->appendStylesheet("/styles/productList.css");
        $sub_category_id = $this->_getParam('sub_category');
        $category_id = $this->_getParam('category');
        $page = $this->_getParam('page');

        if (!is_null($sub_category_id)) {
            $subCategory = $this->_api->Category->find($sub_category_id);
            $topCategory = $this->_api->Category->find($subCategory->parent_id);
        } elseif (!is_null($category_id)) {
            $topCategory = $this->_api->Category->find($category_id);
            $subCategory = $this->_api->Category->getFirstSubCategory($topCategory);
        } else {
            $topCategory = $this->_api->Category->getFirstSubCategory();
            $subCategory = $this->_api->Category->getFirstSubCategory($topCategory);
        }

        $nav = Zend_Registry::get('Zend_Navigation');
        $catalog = $nav->findOneBy('id', 'catalog');
        $catalog->setActive(true);
        $catalog->findOneBy('id', $topCategory->id)->setActive(true);

        $this->view->topCategories = $top = $this->_api->Category->getNested();
        $this->view->menu2 = $top;
        $this->view->api = $this->_api;
        $this->view->menu2active = $topCategory->id;
        $this->view->subCategories = $this->_api->Category->getNested();

        list ($filter, $sort, $is_asc) = $this->_initParams();

        $products = is_null($subCategory) ? array()
                : $this->_api->Product->getFromCategory($subCategory, $filter, $sort, $is_asc);

        $count = count($products);
        $count = ceil($count / Zend_Registry::get('config')->productPerPage);
        $i = 1;
        $k = 0;
        foreach ($products as $product) {
            $k++;
            $prod [$i] [] = $product;
            if ($k == Zend_Registry::get('config')->productPerPage) {
                $i++;
                $k = 0;
            }
        }

        $page = (isset ($page)) ? $page : 1;

        $this->view->products = (isset ($prod [$page])) ? $prod [$page] : array();
        $this->view->count = $count;
        $this->view->page = ( int )$page;
        $this->view->productListTitle = $subCategory->title;

        $this->view->subCategory = $subCategory;
        $this->view->topCategory = $topCategory;
    }

    public function detailsAction()
    {
        $this->view->headLink()->appendStylesheet("/styles/productDetails.css");
        $this->view->headScript()->appendFile("/scripts/gallery.js");
        $product_id = $this->_getParam('product');

        $this->view->api = $this->_api;
        $this->view->product = $this->_api->Product->find(( int )$product_id, true);
        $this->view->subCategory = $this->_api->Category->getByProduct($this->view->product);
        $this->view->topCategory = $this->_api->Category->getParent($this->view->subCategory);
        $this->view->subCategories = $this->_api->Category->getNested($this->view->topCategory);
        $this->view->author = $this->_api->Authors->getOne($this->view->product->author_id);

        list ($filter, $sort, $is_asc) = $this->_initParams();        
        $products = is_null($this->view->subCategory) ? array()
                : $this->_api->Product->getFromCategory($this->view->subCategory, $filter, $sort, $is_asc);
		
        $nextProduct = null;
        foreach ($products as $product) {
        	if (!isset($nextProduct) && $this->view->product->id != $product->id) {
				$preProduct = $product;
			} elseif($this->view->product->id == $product->id) {
				$nextProduct = $product;
			} else {
				$nextProduct = $product;
				break;
			}
        }

        $this->view->preProduct = $preProduct;
        $this->view->nextProduct = $nextProduct;
        
        $nav = Zend_Registry::get('Zend_Navigation');
        $catalog = $nav->findOneBy('id', 'catalog');
        $catalog->setActive(true);
        $catalog->findOneBy('id', $this->view->topCategory->id)->setActive(true);
    }

    public function findAction()
    {
        $this->view->setNoFind = true;
        $this->view->headLink()->appendStylesheet("/styles/productList.css");

        list ($filter, $sort, $is_asc) = $this->_initParams();

        $this->view->productListTitle = $this->view->t('search-page search result');

        $search = ($this->getRequest()->getParam('search'));

        if (is_null($search))
            $search = $this->_getParam('search');
        else {
            $this->getRequest()->setParam('search', $search);
        }
        if (is_null($search)) {
            $this->view->products = null;
            return;
        }
        ;

        $this->view->search = $search;
        if (strlen($search) == 0)
            return;
        $this->view->products = $this->_api->Product->search($search, $filter, $sort, $is_asc);

    }

}
